package com.powernode.controller;

import com.powernode.dto.Result;
import com.powernode.model.CartItem;
import com.powernode.service.CartItemService;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carts")
public class CartItemController {

    @Autowired
    private CartItemService service;

    @GetMapping
    public Result<List<CartItem>> getAll() {
        return Result.success(service.getAll());
    }
    @PostMapping("/add/{productId}")
    public Result addToCart(@PathVariable Long productId){
        return Result.success("添加到购物车成功",service.addToCart(productId));
    }

    @PutMapping("/count/{productId}/{count}")
    public Result updateCount(@PathVariable Long productId,@PathVariable Integer count){
        service.updateCount(productId,count);
        return Result.success("操作成功",null);
    }

    @PutMapping("/checked/{productId}/{checked}")
    public Result checked(@PathVariable Long productId,@PathVariable Boolean checked){
        service.checked(productId,checked);
        return Result.success("操作成功",null);
    }
    @PutMapping("/checkAll/{checked}")
    public Result checkAll(@PathVariable Boolean checked){
        service.checkAll(checked);
        return Result.success("操作成功",null);
    }

    @DeleteMapping("/delete/{productId}")
    public Result deleteFromCart(@PathVariable Long productId){
        service.deleteById(productId);
        return Result.success("删除商品成功",null);
    }

    @DeleteMapping("/deleteAll")
    public Result deleteAll(){
        service.deleteAll();
        return Result.success("清空购物车成功",null);
    }
}
